<template>
  <div>
    <van-nav-bar title="爱阅之城" class="navbar" />
    <van-search
      v-model="value"
      shape="round"
      background="#fffff"
      placeholder="请输入查询书籍"
      @input="searchBook"
    >
    </van-search>
    <div class="wrapper">
      <div class="books">
        <div
          class="item"
          v-for="item in booksInfo"
          :key="item.id"
          @click="toDetail(item.id)"
        >
          <div class="image">
            <img :src="item.image" alt="" />
          </div>
          <div class="author">{{ item.author }}</div>
          <div class="bookName">{{ item.book_name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: "",
      booksInfo: [],
    };
  },
  async created() {
    let res = await this.$http({
      url: "/book",
    });
    console.log(res);
    this.booksInfo = res.bookList;
    this.booksInfo = this.booksInfo.filter((item) => item.enable);
    console.log(this.booksInfo);
  },
  methods: {
    toDetail(id) {
      this.$router.push(`/home/${id}`);
    },
    async searchBook() {
      let res = await this.$http({
        url: "/book/search",
        data: {
          keyValue: this.value,
        },
        method: "POST",
      });
      // console.log(res);
      this.booksInfo = res.bookList;
      this.booksInfo = this.booksInfo.filter((item) => item.enable);
    },
  },
};
</script>

<style scoped>
.navbar {
  position: sticky;
  top: 0;
  left: 0;
}
.search {
  width: 100%;
}
.wrapper {
  padding: 0 15px;
  margin-bottom: 3.125rem;
}
.books {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin-top: 20px;
}
.item {
  width: 47%;
  margin-top: 5px;
}
.item .image img {
  width: 100%;
  height: 12.5rem;
  border-radius: 20px;
}
.author {
  font-size: 14px;
  color: #474749;
}
.bookName {
  font-size: 18px;
  font-weight: 600;
  margin: 2px 0;
}
</style>